package com.dhwl.hsh.merchant.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.resp.UserResponse;
import com.dhwl.hsh.lib.constant.GlobalConfig;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.PreferenceHelper;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.model.impl.UserModel;
import com.dhwl.hsh.merchant.presenter.iview.ILoginView;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-13 12:55
 */
public class LoginPresenter extends BasePresenter<ILoginView> {

    private ReservoirUtils reservoirUtils;

    public LoginPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }

    /**
     * @param account
     * @param pwd
     */
    public void login(String account, String pwd) {
        Subscriber<HttpResponse<UserResponse>> subscriber = new Subscriber<HttpResponse<UserResponse>>() {
            @Override
            public void onCompleted() {
                if (LoginPresenter.this.mCompositeSubscription != null)
                    LoginPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("merchantLogin", e.getMessage());
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
                HshException exception = new HshException(e.hashCode(), e.getMessage());
                LoginPresenter.this.getMvpView().onLoginFailure(exception);

            }

            @Override
            public void onNext(HttpResponse<UserResponse> response) {

                if (response.getStatus() != 0) {
                    ToastUtils.show((Context) getMvpView(), response.getDescribe(), Toast.LENGTH_LONG);
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                    LoginPresenter.this.getMvpView().onLoginFailure(exception);
                } else {
                    ToastUtils.show(HshApplication.mContext, R.string.login_suc, Toast.LENGTH_SHORT);

                    UserResponse userResponse = response.getData();
                    // 保存用户信息
                    reservoirUtils.refresh("user_info",userResponse);
                    GlobalConfig.getInstance().setTokenId(userResponse.getTokenId());
                    PreferenceHelper.write((Context) getMvpView(), Constant.GLOBAL_PREFERENCE_FILE, "is_login", true);


                    LoginPresenter.this.getMvpView().onLoginSuccess(null);
                    //Bugly set user info
                    CrashReport.setUserId(userResponse.getUserId());  //本次启动后的异常日志用户ID都将是9527
                    CrashReport.putUserData((Context) getMvpView(), "phone", userResponse.toString());
                    //umeng 数据账号统计
                    MobclickAgent.onProfileSignIn(userResponse.getUserId());

                }
            }
        };
        this.mCompositeSubscription.add(UserModel.getInstance().login(account, pwd).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
    }
}
